const conn = require("../../libs/DB/Conn");
const _ = require("underscore");
const {createSql, projectDeviceDataUniq, pieTimeRange} = require("./dataAnalysisUtil");

module.exports = async function(params){

    let time = pieTimeRange(params.timeType)
    let sql = createSql(params.type,params.userDomainId,time.type,time.range,{type:"project"},null,null);

    let result = await conn(sql.sql);
    result = projectDeviceDataUniq(result);
    let dataList = [];
    let projectGroup = _.groupBy(result,"projectId");
    for(const projectId in projectGroup){
        let projectData = projectGroup[projectId];
        dataList.push({value:projectData[0].consumption,name:projectData[0].projectName})
    }

    return {success:true,dataList:dataList};

}